

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>quippy.cp2k &mdash; quippy 7ea04c5+ documentation</title>
  

  
  
    <link rel="shortcut icon" href="../../_static/favicon.ico"/>
  
  
  

  
  <script type="text/javascript" src="../../_static/js/modernizr.min.js"></script>
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../" src="../../_static/documentation_options.js"></script>
        <script type="text/javascript" src="../../_static/jquery.js"></script>
        <script type="text/javascript" src="../../_static/underscore.js"></script>
        <script type="text/javascript" src="../../_static/doctools.js"></script>
        <script type="text/javascript" src="../../_static/language_data.js"></script>
        <script async="async" type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
        <script type="text/x-mathjax-config">MathJax.Hub.Config({"tex2jax": {"processClass": "math|output_area", "processEscapes": true, "ignoreClass": "document", "inlineMath": [["$", "$"], ["\\(", "\\)"]]}})</script>
    
    <script type="text/javascript" src="../../_static/js/theme.js"></script>

    

  
  <link rel="stylesheet" href="../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
  <link rel="stylesheet" href="../../_static/theme_overrides.css" type="text/css" />
    <link rel="index" title="Index" href="../../genindex.html" />
    <link rel="search" title="Search" href="../../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../index.html" class="icon icon-home"> quippy
          

          
            
            <img src="../../_static/hybrid.png" class="logo" alt="Logo"/>
          
          </a>

          
            
            
              <div class="version">
                7ea04c5+
              </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
    
            
            
              
            
            
              <ul>
<li class="toctree-l1"><a class="reference internal" href="../../intro.html">Introduction to <code class="docutils literal notranslate"><span class="pre">quippy</span></code></a></li>
<li class="toctree-l1"><a class="reference internal" href="../../install.html">Installation of QUIP and quippy</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../Tutorials/index.html">Tutorials</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../quippy.html">Quippy library reference</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../visualisation.html">Visualisation Tools</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../fortran_wrapper.html">Appendix: wrapping Fortran 90 Code</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../teach_sparse.html">Command line options of the teach_sparse main program</a></li>
</ul>

            
          
    <p></p>
    <ul>
        <li><a href="../../genindex.html">Index</a></li>
        <li><a href="../../py-modindex.html">Module Index</a></li>
        <li><a href="../../search.html">Search Page</a></li>
        <li><a href="../index.html">Module Source Code</a></li>
    </ul>
  
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../index.html">quippy</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../index.html">Module code</a> &raquo;</li>
        
      <li>quippy.cp2k</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for quippy.cp2k</h1><div class="highlight"><pre>
<span></span><span class="c1"># HQ XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX</span>
<span class="c1"># HQ X</span>
<span class="c1"># HQ X   quippy: Python interface to QUIP atomistic simulation library</span>
<span class="c1"># HQ X</span>
<span class="c1"># HQ X   Copyright James Kermode 2010</span>
<span class="c1"># HQ X</span>
<span class="c1"># HQ X   These portions of the source code are released under the GNU General</span>
<span class="c1"># HQ X   Public License, version 2, http://www.gnu.org/copyleft/gpl.html</span>
<span class="c1"># HQ X</span>
<span class="c1"># HQ X   If you would like to license the source code under different terms,</span>
<span class="c1"># HQ X   please contact James Kermode, james.kermode@gmail.com</span>
<span class="c1"># HQ X</span>
<span class="c1"># HQ X   When using this software, please cite the following reference:</span>
<span class="c1"># HQ X</span>
<span class="c1"># HQ X   http://www.jrkermode.co.uk/quippy</span>
<span class="c1"># HQ X</span>
<span class="c1"># HQ XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX</span>

<span class="kn">import</span> <span class="nn">sys</span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">from</span> <span class="nn">quippy.system</span> <span class="k">import</span> <span class="n">verbosity_push</span><span class="p">,</span> <span class="n">verbosity_pop</span><span class="p">,</span> <span class="n">PRINT_SILENT</span>
<span class="kn">from</span> <span class="nn">quippy.atoms</span> <span class="k">import</span> <span class="n">Atoms</span>
<span class="kn">from</span> <span class="nn">quippy.io</span> <span class="k">import</span> <span class="n">atoms_reader</span>
<span class="kn">from</span> <span class="nn">quippy.farray</span> <span class="k">import</span> <span class="n">fzeros</span><span class="p">,</span> <span class="n">s2a</span><span class="p">,</span> <span class="n">farray</span><span class="p">,</span> <span class="n">fenumerate</span><span class="p">,</span> <span class="n">frange</span>
<span class="kn">from</span> <span class="nn">quippy.table</span> <span class="k">import</span> <span class="n">TABLE_STRING_LENGTH</span>
<span class="kn">from</span> <span class="nn">quippy.units</span> <span class="k">import</span> <span class="n">MASSCONVERT</span><span class="p">,</span> <span class="n">HARTREE</span><span class="p">,</span> <span class="n">BOHR</span>
<span class="kn">from</span> <span class="nn">quippy.clusters</span> <span class="k">import</span> <span class="n">HYBRID_NO_MARK</span>
<span class="kn">from</span> <span class="nn">quippy.potential</span> <span class="k">import</span> <span class="n">Potential</span>
<span class="kn">from</span> <span class="nn">quippy.ordereddict</span> <span class="k">import</span> <span class="n">OrderedDict</span>
<span class="kn">from</span> <span class="nn">quippy.util</span> <span class="k">import</span> <span class="n">read_text_file</span>
<span class="kn">from</span> <span class="nn">quippy.cp2k_driver</span> <span class="k">import</span> <span class="n">do_cp2k_calc</span><span class="p">,</span> <span class="n">read_output</span><span class="p">,</span> <span class="n">qmmm_qm_abc</span>

<span class="n">__all__</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;CP2KPotential&#39;</span><span class="p">,</span> <span class="s1">&#39;CP2KInputHeader&#39;</span><span class="p">,</span> <span class="s1">&#39;read_cp2k_qm_kind&#39;</span><span class="p">,</span>
           <span class="s1">&#39;read_psf&#39;</span><span class="p">,</span> <span class="s1">&#39;apply_cp2k_sort_order&#39;</span><span class="p">,</span> <span class="s1">&#39;bonds_from_avgpos&#39;</span><span class="p">,</span>
           <span class="s1">&#39;qmmm_link_bonds&#39;</span><span class="p">]</span>

<div class="viewcode-block" id="CP2KPotential"><a class="viewcode-back" href="../../cp2k.html#quippy.cp2k.CP2KPotential">[docs]</a><span class="k">class</span> <span class="nc">CP2KPotential</span><span class="p">(</span><span class="n">Potential</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    QUIP Potential interface to the CP2K code.</span>

<span class="sd">    Calls do_cp2k_calc() in QUIP_FilePot_Drivers/cp2k_driver_module.f95 to do the heavy lifting</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span>
                 <span class="n">fpointer</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">finalise</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
                 <span class="n">error</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="n">Potential</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">callback</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">do_cp2k_calc</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">do_cp2k_calc</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">at</span><span class="p">):</span>
        <span class="n">args_str</span> <span class="o">=</span> <span class="n">at</span><span class="o">.</span><span class="n">params</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;calc_args_str&#39;</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">)</span>
        <span class="n">cp2k_force</span><span class="p">,</span> <span class="n">cp2k_energy</span> <span class="o">=</span> <span class="n">do_cp2k_calc</span><span class="p">(</span><span class="n">at</span><span class="p">,</span> <span class="n">infile</span><span class="o">=</span><span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="n">args_str</span><span class="o">=</span><span class="n">args_str</span><span class="p">,</span> <span class="n">n0</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span> <span class="n">n1</span><span class="o">=</span><span class="n">at</span><span class="o">.</span><span class="n">n</span><span class="p">)</span>
        <span class="n">at</span><span class="o">.</span><span class="n">add_property</span><span class="p">(</span><span class="s1">&#39;force&#39;</span><span class="p">,</span> <span class="n">cp2k_force</span><span class="p">,</span> <span class="n">overwrite</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
        <span class="n">at</span><span class="o">.</span><span class="n">params</span><span class="p">[</span><span class="s1">&#39;energy&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">cp2k_energy</span></div>


<div class="viewcode-block" id="CP2KInputHeader"><a class="viewcode-back" href="../../cp2k.html#quippy.cp2k.CP2KInputHeader">[docs]</a><span class="k">class</span> <span class="nc">CP2KInputHeader</span><span class="p">(</span><span class="n">OrderedDict</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Read a cp2k_input.inp.header file and parse into an ordered dictionary.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">filename</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="n">OrderedDict</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">filename</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">read</span><span class="p">(</span><span class="n">filename</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">read</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">filename</span><span class="p">):</span>
        <span class="n">filename</span><span class="p">,</span> <span class="n">lines</span> <span class="o">=</span> <span class="n">read_text_file</span><span class="p">(</span><span class="n">filename</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">lines</span><span class="p">:</span>
            <span class="n">dummy</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">value</span> <span class="o">=</span> <span class="n">line</span><span class="o">.</span><span class="n">split</span><span class="p">()</span>
            <span class="bp">self</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="n">value</span>

    <span class="k">def</span> <span class="nf">write</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">filename</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">filename</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">filename</span> <span class="o">=</span> <span class="n">sys</span><span class="o">.</span><span class="n">stdout</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="n">basestring</span><span class="p">):</span>
            <span class="n">file</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="s1">&#39;w&#39;</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">file</span> <span class="o">=</span> <span class="n">filename</span>
    
        <span class="k">for</span> <span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">iteritems</span><span class="p">():</span>
            <span class="n">file</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s1">&#39;@SET </span><span class="si">%s</span><span class="s1"> </span><span class="si">%s</span><span class="se">\n</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="n">value</span><span class="p">))</span>
            
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="n">basestring</span><span class="p">):</span>            
            <span class="n">file</span><span class="o">.</span><span class="n">close</span><span class="p">()</span></div>


<span class="k">def</span> <span class="nf">cp2k_run_type</span><span class="p">(</span><span class="n">cp2k_output</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">cp2k_input_header</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Find the run type of the CP2K calculation.</span>

<span class="sd">    Either `cp2k_output` or `cp2k_input_header` should be present.</span>
<span class="sd">    When both are givenm we check output is consistent with input,</span>
<span class="sd">    raising a ValueError if not.</span>

<span class="sd">    Returns one of &#39;QS&#39;, &#39;MM&#39; or &#39;QMMM&#39;.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">if</span> <span class="n">cp2k_output</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">cp2k_input_header</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;atlest one of cp2k_output and cp2k_input_header must be present&quot;</span><span class="p">)</span>

    <span class="k">if</span> <span class="n">cp2k_output</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">got_fist</span> <span class="o">=</span> <span class="s2">&quot; MODULE FIST:  ATOMIC COORDINATES IN angstrom</span><span class="se">\n</span><span class="s2">&quot;</span> <span class="ow">in</span> <span class="n">cp2k_output</span>
        <span class="n">got_qs</span>   <span class="o">=</span> <span class="s2">&quot; MODULE QUICKSTEP:  ATOMIC COORDINATES IN angstrom</span><span class="se">\n</span><span class="s2">&quot;</span> <span class="ow">in</span> <span class="n">cp2k_output</span>
        <span class="n">got_qmmm</span> <span class="o">=</span> <span class="s2">&quot; MODULE QM/MM:  ATOMIC COORDINATES IN angstrom</span><span class="se">\n</span><span class="s2">&quot;</span> <span class="ow">in</span> <span class="n">cp2k_output</span>
        <span class="n">got_modules</span> <span class="o">=</span> <span class="p">(</span><span class="n">got_fist</span><span class="p">,</span> <span class="n">got_qs</span><span class="p">,</span> <span class="n">got_qmmm</span><span class="p">)</span>

    <span class="k">if</span> <span class="n">cp2k_input_header</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">do_fist</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">cp2k_input_header</span><span class="p">[</span><span class="s1">&#39;DO_MM&#39;</span><span class="p">])</span>
        <span class="n">do_qs</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">cp2k_input_header</span><span class="p">[</span><span class="s1">&#39;DO_DFT&#39;</span><span class="p">])</span>
        <span class="n">do_qmmm</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">cp2k_input_header</span><span class="p">[</span><span class="s1">&#39;DO_QMMM&#39;</span><span class="p">])</span>
        <span class="n">got_modules</span> <span class="o">=</span> <span class="p">(</span><span class="n">do_fist</span><span class="p">,</span> <span class="n">do_qs</span><span class="p">,</span> <span class="n">do_qmmm</span><span class="p">)</span>

    <span class="k">if</span> <span class="n">cp2k_output</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">cp2k_input_header</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">do_fist</span> <span class="o">!=</span> <span class="n">got_fist</span> <span class="ow">or</span> <span class="n">do_qs</span> <span class="o">!=</span> <span class="n">got_qs</span> <span class="ow">or</span> <span class="n">do_qmmm</span> <span class="o">!=</span> <span class="n">got_qmmm</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;CP2K output inconsitent with cp2k input header&quot;</span><span class="p">)</span>

    <span class="k">if</span> <span class="n">got_modules</span> <span class="o">==</span> <span class="p">(</span><span class="kc">True</span><span class="p">,</span> <span class="kc">True</span><span class="p">,</span> <span class="kc">True</span><span class="p">):</span>
        <span class="k">return</span> <span class="s2">&quot;QMMM&quot;</span>
    <span class="k">elif</span> <span class="n">got_modules</span> <span class="o">==</span> <span class="p">(</span><span class="kc">True</span><span class="p">,</span> <span class="kc">False</span><span class="p">,</span> <span class="kc">False</span><span class="p">):</span>
        <span class="k">return</span> <span class="s2">&quot;MM&quot;</span>
    <span class="k">elif</span> <span class="n">got_modules</span> <span class="o">==</span> <span class="p">(</span><span class="kc">False</span><span class="p">,</span> <span class="kc">True</span><span class="p">,</span> <span class="kc">False</span><span class="p">):</span>
        <span class="k">return</span> <span class="s2">&quot;QS&quot;</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Got inconsistent set of modules got_fist=</span><span class="si">%s</span><span class="s2">, got_qs=</span><span class="si">%s</span><span class="s2">, got_qmmm+</span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="n">got_modules</span><span class="p">)</span>
    

<span class="nd">@atoms_reader</span><span class="p">(</span><span class="s1">&#39;cp2k_output.out&#39;</span><span class="p">)</span>
<span class="nd">@atoms_reader</span><span class="p">(</span><span class="s1">&#39;cp2k_output&#39;</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">CP2KOutputReader</span><span class="p">(</span><span class="n">fh</span><span class="p">,</span> <span class="n">module</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">type_map</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">kind_map</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="nb">format</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>

    <span class="c1"># mapping from run type to (default module index, list of available module)</span>
    <span class="n">run_types</span> <span class="o">=</span> <span class="p">{</span>
        <span class="s1">&#39;QS&#39;</span><span class="p">:</span>   <span class="p">[</span><span class="s1">&#39;QUICKSTEP&#39;</span><span class="p">],</span>
        <span class="s1">&#39;QMMM&#39;</span><span class="p">:</span> <span class="p">[</span><span class="s1">&#39;FIST&#39;</span><span class="p">,</span> <span class="s1">&#39;QM/MM&#39;</span><span class="p">,</span> <span class="s1">&#39;QUICKSTEP&#39;</span><span class="p">],</span>
        <span class="s1">&#39;MM&#39;</span> <span class="p">:</span>  <span class="p">[</span><span class="s1">&#39;FIST&#39;</span><span class="p">]</span>
        <span class="p">}</span>

    <span class="n">filename</span><span class="p">,</span> <span class="n">lines</span> <span class="o">=</span> <span class="n">read_text_file</span><span class="p">(</span><span class="n">fh</span><span class="p">)</span>
    <span class="n">run_type</span> <span class="o">=</span> <span class="n">cp2k_run_type</span><span class="p">(</span><span class="n">cp2k_output</span><span class="o">=</span><span class="n">lines</span><span class="p">)</span>
    
    <span class="k">if</span> <span class="n">type_map</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">type_map</span> <span class="o">=</span> <span class="p">{}</span>
    <span class="k">if</span> <span class="n">kind_map</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">kind_map</span> <span class="o">=</span> <span class="p">{}</span>

    <span class="k">try</span><span class="p">:</span>
        <span class="n">available_modules</span> <span class="o">=</span> <span class="n">run_types</span><span class="p">[</span><span class="n">run_type</span><span class="p">]</span>
    <span class="k">except</span> <span class="ne">KeyError</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;Unknown CP2K run type </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">run_type</span><span class="p">)</span>

    <span class="k">if</span> <span class="n">module</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">module</span> <span class="o">=</span> <span class="n">available_modules</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>

    <span class="k">try</span><span class="p">:</span>
        <span class="n">cell_index</span> <span class="o">=</span> <span class="n">available_modules</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="n">module</span><span class="p">)</span>
    <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Don&#39;t know how to read module </span><span class="si">%s</span><span class="s2"> from file </span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">module</span><span class="p">,</span> <span class="n">filename</span><span class="p">))</span>

    <span class="n">cell_lines</span> <span class="o">=</span> <span class="p">[</span><span class="n">i</span> <span class="k">for</span> <span class="n">i</span><span class="p">,</span><span class="n">line</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">lines</span><span class="p">)</span> <span class="k">if</span> <span class="n">line</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s2">&quot; CELL| Vector a&quot;</span><span class="p">)]</span>
    <span class="k">if</span> <span class="n">cell_lines</span> <span class="o">==</span> <span class="p">[]:</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Cannot find cell in file </span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="n">filename</span><span class="p">)</span>

    <span class="k">try</span><span class="p">:</span>
        <span class="n">cell_line</span> <span class="o">=</span> <span class="n">cell_lines</span><span class="p">[</span><span class="n">cell_index</span><span class="p">]</span>
    <span class="k">except</span> <span class="ne">IndexError</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Cannot find cell with index </span><span class="si">%d</span><span class="s2"> in file </span><span class="si">%s</span><span class="s2"> for module </span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span>
                         <span class="p">(</span><span class="n">cell_index</span><span class="p">,</span> <span class="n">filename</span><span class="p">,</span> <span class="n">module</span><span class="p">))</span>

    <span class="n">lattice</span> <span class="o">=</span> <span class="n">fzeros</span><span class="p">((</span><span class="mi">3</span><span class="p">,</span><span class="mi">3</span><span class="p">))</span>
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">]:</span>
        <span class="n">lattice</span><span class="p">[:,</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="nb">float</span><span class="p">(</span><span class="n">c</span><span class="p">)</span> <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="n">lines</span><span class="p">[</span><span class="n">cell_line</span><span class="o">+</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">split</span><span class="p">()[</span><span class="mi">4</span><span class="p">:</span><span class="mi">7</span><span class="p">]]</span>
        
    <span class="k">try</span><span class="p">:</span>
        <span class="n">start_line</span> <span class="o">=</span> <span class="n">lines</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="s2">&quot; MODULE </span><span class="si">%s</span><span class="s2">:  ATOMIC COORDINATES IN angstrom</span><span class="se">\n</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="n">module</span><span class="p">)</span>
    <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Cannot find atomic positions for module </span><span class="si">%s</span><span class="s2"> in file </span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">module</span><span class="p">,</span> <span class="n">filename</span><span class="p">))</span>

    <span class="n">kinds</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="n">species</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="n">Zs</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="n">pos</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="n">masses</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="n">Zeffs</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="n">types</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="n">qeffs</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">lines</span><span class="p">[</span><span class="n">start_line</span><span class="o">+</span><span class="mi">4</span><span class="p">:]:</span>
        <span class="k">if</span> <span class="n">line</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span> <span class="o">==</span> <span class="s1">&#39;&#39;</span><span class="p">:</span>
            <span class="k">break</span>
        <span class="k">if</span> <span class="n">module</span> <span class="o">==</span> <span class="s1">&#39;FIST&#39;</span><span class="p">:</span>
            <span class="n">atom</span><span class="p">,</span> <span class="n">kind</span><span class="p">,</span> <span class="n">typ</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span><span class="p">,</span> <span class="n">qeff</span><span class="p">,</span> <span class="n">mass</span> <span class="o">=</span> <span class="n">line</span><span class="o">.</span><span class="n">split</span><span class="p">()</span>
            <span class="n">types</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">typ</span><span class="p">)</span>
            <span class="n">Z</span> <span class="o">=</span> <span class="n">type_map</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">typ</span><span class="p">,</span><span class="mi">0</span><span class="p">)</span>
            <span class="n">kind</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">kind</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">Z</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                <span class="n">Z</span> <span class="o">=</span> <span class="n">kind_map</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">kind</span><span class="p">,</span><span class="mi">0</span><span class="p">)</span>
            <span class="n">Zs</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">Z</span><span class="p">)</span>
            <span class="n">qeffs</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">float</span><span class="p">(</span><span class="n">qeff</span><span class="p">))</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">atom</span><span class="p">,</span> <span class="n">kind</span><span class="p">,</span> <span class="n">sp</span><span class="p">,</span> <span class="n">Z</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span><span class="p">,</span> <span class="n">Zeff</span><span class="p">,</span> <span class="n">mass</span> <span class="o">=</span> <span class="n">line</span><span class="o">.</span><span class="n">split</span><span class="p">()</span>
            <span class="n">species</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">sp</span><span class="p">)</span>
            <span class="n">Zs</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">Z</span><span class="p">))</span>
            <span class="n">Zeffs</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">float</span><span class="p">(</span><span class="n">Zeff</span><span class="p">))</span>
        <span class="n">kinds</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">kind</span><span class="p">))</span>
        <span class="n">pos</span><span class="o">.</span><span class="n">append</span><span class="p">([</span><span class="nb">float</span><span class="p">(</span><span class="n">x</span><span class="p">),</span><span class="nb">float</span><span class="p">(</span><span class="n">y</span><span class="p">),</span><span class="nb">float</span><span class="p">(</span><span class="n">z</span><span class="p">)])</span>
        <span class="n">masses</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">float</span><span class="p">(</span><span class="n">mass</span><span class="p">))</span>

    <span class="n">at</span> <span class="o">=</span> <span class="n">Atoms</span><span class="p">(</span><span class="n">n</span><span class="o">=</span><span class="nb">len</span><span class="p">(</span><span class="n">kinds</span><span class="p">),</span> <span class="n">lattice</span><span class="o">=</span><span class="n">lattice</span><span class="p">)</span>
    <span class="n">at</span><span class="o">.</span><span class="n">pos</span><span class="p">[</span><span class="o">...</span><span class="p">]</span> <span class="o">=</span> <span class="n">farray</span><span class="p">(</span><span class="n">pos</span><span class="p">)</span><span class="o">.</span><span class="n">T</span>
    <span class="n">at</span><span class="o">.</span><span class="n">set_atoms</span><span class="p">(</span><span class="n">Zs</span><span class="p">)</span>
    <span class="n">at</span><span class="o">.</span><span class="n">add_property</span><span class="p">(</span><span class="s1">&#39;mass&#39;</span><span class="p">,</span> <span class="n">farray</span><span class="p">(</span><span class="n">masses</span><span class="p">)</span><span class="o">*</span><span class="n">MASSCONVERT</span><span class="p">)</span>
    <span class="n">at</span><span class="o">.</span><span class="n">add_property</span><span class="p">(</span><span class="s1">&#39;kind&#39;</span><span class="p">,</span> <span class="n">kinds</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">module</span> <span class="o">==</span> <span class="s1">&#39;FIST&#39;</span><span class="p">:</span>
        <span class="n">at</span><span class="o">.</span><span class="n">add_property</span><span class="p">(</span><span class="s1">&#39;type&#39;</span><span class="p">,</span> <span class="s1">&#39; &#39;</span><span class="o">*</span><span class="n">TABLE_STRING_LENGTH</span><span class="p">)</span>
        <span class="n">at</span><span class="o">.</span><span class="n">add_property</span><span class="p">(</span><span class="s1">&#39;qm&#39;</span><span class="p">,</span> <span class="kc">False</span><span class="p">)</span>
        <span class="n">at</span><span class="o">.</span><span class="n">qm</span><span class="p">[:]</span> <span class="o">=</span> <span class="p">(</span><span class="n">at</span><span class="o">.</span><span class="n">type</span><span class="o">.</span><span class="n">stripstrings</span><span class="p">()</span> <span class="o">==</span> <span class="s1">&#39;_QM_&#39;</span><span class="p">)</span> <span class="o">|</span> <span class="p">(</span><span class="n">at</span><span class="o">.</span><span class="n">type</span><span class="o">.</span><span class="n">stripstrings</span><span class="p">()</span> <span class="o">==</span> <span class="s1">&#39;_LNK&#39;</span><span class="p">)</span>
        <span class="n">at</span><span class="o">.</span><span class="n">type</span><span class="p">[</span><span class="o">...</span><span class="p">]</span> <span class="o">=</span> <span class="n">s2a</span><span class="p">(</span><span class="n">types</span><span class="p">,</span> <span class="n">TABLE_STRING_LENGTH</span><span class="p">)</span>
        <span class="n">at</span><span class="o">.</span><span class="n">add_property</span><span class="p">(</span><span class="s1">&#39;qeff&#39;</span><span class="p">,</span> <span class="n">qeffs</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">at</span><span class="o">.</span><span class="n">species</span><span class="p">[</span><span class="o">...</span><span class="p">]</span> <span class="o">=</span> <span class="n">s2a</span><span class="p">(</span><span class="n">species</span><span class="p">,</span> <span class="n">TABLE_STRING_LENGTH</span><span class="p">)</span>
        <span class="n">at</span><span class="o">.</span><span class="n">add_property</span><span class="p">(</span><span class="s1">&#39;zeff&#39;</span><span class="p">,</span> <span class="n">Zeffs</span><span class="p">)</span>

    <span class="k">yield</span> <span class="n">at</span>


<span class="nd">@atoms_reader</span><span class="p">(</span><span class="s1">&#39;cp2k_run_dir&#39;</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">CP2KDirectoryReader</span><span class="p">(</span><span class="n">run_dir</span><span class="p">,</span> <span class="n">at_ref</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">proj</span><span class="o">=</span><span class="s1">&#39;quip&#39;</span><span class="p">,</span> <span class="n">calc_qm_charges</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                        <span class="n">calc_virial</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">out_i</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">qm_vacuum</span><span class="o">=</span><span class="mf">6.0</span><span class="p">,</span> <span class="n">run_suffix</span><span class="o">=</span><span class="s1">&#39;_extended&#39;</span><span class="p">,</span> <span class="nb">format</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    <span class="k">if</span> <span class="n">at_ref</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">filepot_xyz</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">run_dir</span><span class="p">,</span> <span class="s1">&#39;filepot.xyz&#39;</span><span class="p">)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">filepot_xyz</span><span class="p">):</span>
            <span class="c1"># try looking up one level </span>
            <span class="n">filepot_xyz</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">run_dir</span><span class="p">,</span> <span class="s1">&#39;../filepot.xyz&#39;</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">filepot_xyz</span><span class="p">):</span>
            <span class="n">at_ref</span> <span class="o">=</span> <span class="n">Atoms</span><span class="p">(</span><span class="n">filepot_xyz</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">at_ref</span> <span class="o">=</span> <span class="n">Atoms</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">run_dir</span><span class="p">,</span> <span class="s1">&#39;cp2k_output.out&#39;</span><span class="p">),</span>
                           <span class="nb">format</span><span class="o">=</span><span class="s1">&#39;cp2k_output&#39;</span><span class="p">)</span>

    <span class="n">at</span> <span class="o">=</span> <span class="n">at_ref</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>

    <span class="n">cp2k_output_filename</span><span class="p">,</span> <span class="n">cp2k_output</span> <span class="o">=</span> <span class="n">read_text_file</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">run_dir</span><span class="p">,</span> <span class="s1">&#39;cp2k_output.out&#39;</span><span class="p">))</span>
    <span class="n">cp2k_params</span> <span class="o">=</span> <span class="n">CP2KInputHeader</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">run_dir</span><span class="p">,</span> <span class="s1">&#39;cp2k_input.inp.header&#39;</span><span class="p">))</span>
    <span class="n">at</span><span class="o">.</span><span class="n">params</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">cp2k_params</span><span class="p">)</span>

    <span class="n">run_type</span> <span class="o">=</span> <span class="n">cp2k_run_type</span><span class="p">(</span><span class="n">cp2k_output</span><span class="o">=</span><span class="n">cp2k_output</span><span class="p">,</span> <span class="n">cp2k_input_header</span><span class="o">=</span><span class="n">cp2k_params</span><span class="p">)</span>

    <span class="k">try</span><span class="p">:</span>
        <span class="n">cluster_mark</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">at</span><span class="p">,</span> <span class="s1">&#39;cluster_mark&#39;</span><span class="o">+</span><span class="n">run_suffix</span><span class="p">)</span>
        <span class="n">qm_list_a</span> <span class="o">=</span> <span class="p">((</span><span class="n">cluster_mark</span> <span class="o">!=</span> <span class="n">HYBRID_NO_MARK</span><span class="p">)</span><span class="o">.</span><span class="n">nonzero</span><span class="p">()[</span><span class="mi">0</span><span class="p">])</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">int32</span><span class="p">)</span>
    <span class="k">except</span> <span class="ne">AttributeError</span><span class="p">:</span>
        <span class="n">qm_list_a</span> <span class="o">=</span> <span class="n">fzeros</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">int32</span><span class="p">)</span>

    <span class="k">if</span> <span class="n">calc_qm_charges</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">calc_qm_charges</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span>

    <span class="k">try</span><span class="p">:</span>
        <span class="n">cur_qmmm_qm_abc</span> <span class="o">=</span> <span class="p">[</span><span class="nb">float</span><span class="p">(</span><span class="n">cp2k_params</span><span class="p">[</span><span class="s1">&#39;QMMM_ABC_X&#39;</span><span class="p">]),</span>
                           <span class="nb">float</span><span class="p">(</span><span class="n">cp2k_params</span><span class="p">[</span><span class="s1">&#39;QMMM_ABC_Y&#39;</span><span class="p">]),</span>
                           <span class="nb">float</span><span class="p">(</span><span class="n">cp2k_params</span><span class="p">[</span><span class="s1">&#39;QMMM_ABC_Z&#39;</span><span class="p">])]</span>
    <span class="k">except</span> <span class="ne">KeyError</span><span class="p">:</span>
        <span class="k">if</span> <span class="s1">&#39;QM_cell&#39;</span><span class="o">+</span><span class="n">run_suffix</span> <span class="ow">in</span> <span class="n">at</span><span class="o">.</span><span class="n">params</span><span class="p">:</span>
            <span class="n">cur_qmmm_qm_abc</span> <span class="o">=</span> <span class="n">at</span><span class="o">.</span><span class="n">params</span><span class="p">[</span><span class="s1">&#39;QM_cell&#39;</span><span class="o">+</span><span class="n">run_suffix</span><span class="p">]</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">cur_qmmm_qm_abc</span> <span class="o">=</span> <span class="n">qmmm_qm_abc</span><span class="p">(</span><span class="n">at</span><span class="p">,</span> <span class="n">qm_list_a</span><span class="p">,</span> <span class="n">qm_vacuum</span><span class="p">)</span>

    <span class="n">quip_cp2k_at</span> <span class="o">=</span> <span class="n">Atoms</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">run_dir</span><span class="p">,</span> <span class="s1">&#39;quip_cp2k.xyz&#39;</span><span class="p">))</span>

    <span class="n">rev_sort_index_file</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">run_dir</span><span class="p">,</span> <span class="s1">&#39;../quip_rev_sort_index&#39;</span><span class="p">)</span>
    <span class="n">fields</span> <span class="o">=</span> <span class="p">[</span><span class="nb">int</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="nb">open</span><span class="p">(</span><span class="n">rev_sort_index_file</span><span class="p">)</span><span class="o">.</span><span class="n">read</span><span class="p">()</span><span class="o">.</span><span class="n">split</span><span class="p">()</span> <span class="p">]</span>
    <span class="n">rev_sort_index</span> <span class="o">=</span> <span class="n">farray</span><span class="p">(</span><span class="n">fields</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">int32</span><span class="p">)</span>
    <span class="c1">#verbosity_push(PRINT_SILENT)</span>
    <span class="n">cp2k_energy</span><span class="p">,</span> <span class="n">cp2k_force</span> <span class="o">=</span> <span class="n">read_output</span><span class="p">(</span><span class="n">quip_cp2k_at</span><span class="p">,</span> <span class="n">qm_list_a</span><span class="p">,</span> <span class="n">cur_qmmm_qm_abc</span><span class="p">,</span> <span class="n">run_dir</span><span class="p">,</span> <span class="n">proj</span><span class="p">,</span>
                                          <span class="n">calc_qm_charges</span><span class="p">,</span> <span class="n">calc_virial</span><span class="p">,</span> <span class="kc">True</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="n">at</span><span class="o">.</span><span class="n">n</span><span class="p">,</span> <span class="n">out_i</span><span class="p">)</span>
    <span class="c1">#verbosity_pop()</span>

    <span class="n">qm_list</span> <span class="o">=</span> <span class="kc">None</span>
    <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">run_dir</span><span class="p">,</span> <span class="s1">&#39;cp2k_input.qmmm_qm_kind&#39;</span><span class="p">)):</span>
        <span class="n">qm_kind_grep_cmd</span> <span class="o">=</span> <span class="s2">&quot;grep MM_INDEX </span><span class="si">%s</span><span class="s2">/cp2k_input.qmmm_qm_kind | awk &#39;{print $2}&#39;&quot;</span> <span class="o">%</span> <span class="n">run_dir</span>
        <span class="n">qm_list</span> <span class="o">=</span> <span class="p">[</span><span class="nb">int</span><span class="p">(</span><span class="n">i</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">os</span><span class="o">.</span><span class="n">popen</span><span class="p">(</span><span class="n">qm_kind_grep_cmd</span><span class="p">)</span><span class="o">.</span><span class="n">read</span><span class="p">()</span><span class="o">.</span><span class="n">split</span><span class="p">()]</span>

    <span class="k">if</span> <span class="n">qm_list</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">run_type</span> <span class="o">==</span> <span class="s1">&#39;QMMM&#39;</span><span class="p">:</span>
            <span class="n">reordering_index</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">at</span><span class="p">,</span> <span class="s1">&#39;reordering_index&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>

            <span class="n">at</span><span class="o">.</span><span class="n">add_property</span><span class="p">(</span><span class="s1">&#39;qm&#39;</span><span class="p">,</span> <span class="kc">False</span><span class="p">,</span> <span class="n">overwrite</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">reordering_index</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">qm_list</span> <span class="o">=</span> <span class="n">reordering_index</span><span class="p">[</span><span class="n">qm_list</span><span class="p">]</span>
            <span class="n">at</span><span class="o">.</span><span class="n">qm</span><span class="p">[</span><span class="n">qm_list</span><span class="p">]</span> <span class="o">=</span> <span class="kc">True</span>
        <span class="k">elif</span> <span class="n">run_type</span> <span class="o">==</span> <span class="s1">&#39;QS&#39;</span><span class="p">:</span>
            <span class="n">at</span><span class="o">.</span><span class="n">add_property</span><span class="p">(</span><span class="s1">&#39;qm_orig_index&#39;</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">overwrite</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
            <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">qm_at</span> <span class="ow">in</span> <span class="n">fenumerate</span><span class="p">(</span><span class="n">qm_list</span><span class="p">):</span>
                <span class="n">at</span><span class="o">.</span><span class="n">qm_orig_index</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">sort_index</span><span class="p">[</span><span class="n">qm_at</span><span class="p">]</span>

            
        

    <span class="n">at</span><span class="o">.</span><span class="n">add_property</span><span class="p">(</span><span class="s1">&#39;force&#39;</span><span class="p">,</span> <span class="n">cp2k_force</span><span class="p">,</span> <span class="n">overwrite</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
    <span class="n">at</span><span class="o">.</span><span class="n">params</span><span class="p">[</span><span class="s1">&#39;energy&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">cp2k_energy</span>
    <span class="k">yield</span> <span class="n">at</span>



<div class="viewcode-block" id="read_cp2k_qm_kind"><a class="viewcode-back" href="../../cp2k.html#quippy.cp2k.read_cp2k_qm_kind">[docs]</a><span class="k">def</span> <span class="nf">read_cp2k_qm_kind</span><span class="p">(</span><span class="n">fh</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Read a file in cp2k_input.qmmm_qm_kind</span>

<span class="sd">    This is the format produced by the QUIP cp2k_driver.</span>
<span class="sd">    Returns a list of tuples (qm_kind, atom_indices).</span>
<span class="sd">    &quot;&quot;&quot;</span>
    
    <span class="n">filename</span><span class="p">,</span> <span class="n">lines</span> <span class="o">=</span> <span class="n">read_text_file</span><span class="p">(</span><span class="n">fh</span><span class="p">)</span>
    <span class="n">qm_kinds</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="n">current_qm_kind</span> <span class="o">=</span> <span class="kc">None</span>
    <span class="n">current_indices</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">lines</span><span class="p">:</span>
        <span class="n">line</span> <span class="o">=</span> <span class="n">line</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>
        <span class="k">if</span> <span class="n">line</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;&amp;QM_KIND&#39;</span><span class="p">):</span>
            <span class="n">current_qm_kind</span> <span class="o">=</span> <span class="n">line</span><span class="o">.</span><span class="n">split</span><span class="p">()[</span><span class="mi">1</span><span class="p">]</span>
            <span class="n">current_indices</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">elif</span> <span class="n">line</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;&amp;END QM_KIND&#39;</span><span class="p">):</span>
            <span class="n">qm_kinds</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">current_qm_kind</span><span class="p">,</span><span class="n">current_indices</span><span class="p">))</span>
        <span class="k">elif</span> <span class="n">line</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;MM_INDEX&#39;</span><span class="p">):</span>
            <span class="n">index</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">line</span><span class="o">.</span><span class="n">split</span><span class="p">()[</span><span class="mi">1</span><span class="p">])</span>
            <span class="n">current_indices</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">index</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;Unexpected line </span><span class="si">%r</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">line</span><span class="p">)</span>

    <span class="k">return</span> <span class="n">qm_kinds</span></div>
    
<span class="k">def</span> <span class="nf">read_psf_bonds</span><span class="p">(</span><span class="n">lines</span><span class="p">,</span> <span class="n">rev_sort_index</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    <span class="n">i</span> <span class="o">=</span> <span class="p">(</span><span class="n">i</span> <span class="k">for</span> <span class="n">i</span><span class="p">,</span><span class="n">line</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">lines</span><span class="p">)</span> <span class="k">if</span> <span class="n">line</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s1">&#39;!NBOND</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">))</span><span class="o">.</span><span class="n">next</span><span class="p">()</span>
    <span class="n">nbond</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">lines</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">split</span><span class="p">()[</span><span class="mi">0</span><span class="p">])</span>
    <span class="n">bonds</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="k">while</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="nb">len</span><span class="p">(</span><span class="n">lines</span><span class="p">)</span> <span class="ow">and</span> <span class="nb">len</span><span class="p">(</span><span class="n">bonds</span><span class="p">)</span> <span class="o">&lt;</span> <span class="n">nbond</span><span class="p">:</span>
        <span class="n">i</span> <span class="o">+=</span> <span class="mi">1</span>
        <span class="n">fields</span> <span class="o">=</span> <span class="n">lines</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">split</span><span class="p">()</span>
        <span class="k">for</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">fields</span><span class="p">[::</span><span class="mi">2</span><span class="p">],</span> <span class="n">fields</span><span class="p">[</span><span class="mi">1</span><span class="p">::</span><span class="mi">2</span><span class="p">]):</span>
            <span class="n">a</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
            <span class="n">b</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">b</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">rev_sort_index</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">a</span> <span class="o">=</span> <span class="n">rev_sort_index</span><span class="p">[</span><span class="n">a</span><span class="p">]</span>
                <span class="n">b</span> <span class="o">=</span> <span class="n">rev_sort_index</span><span class="p">[</span><span class="n">b</span><span class="p">]</span>
            <span class="n">bonds</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">a</span><span class="p">,</span><span class="n">b</span><span class="p">))</span>

    <span class="k">return</span> <span class="n">bonds</span>

<span class="k">def</span> <span class="nf">read_psf_angles</span><span class="p">(</span><span class="n">lines</span><span class="p">,</span> <span class="n">rev_sort_index</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    <span class="n">i</span> <span class="o">=</span> <span class="p">(</span><span class="n">i</span> <span class="k">for</span> <span class="n">i</span><span class="p">,</span><span class="n">line</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">lines</span><span class="p">)</span> <span class="k">if</span> <span class="n">line</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s1">&#39;!NTHETA</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">))</span><span class="o">.</span><span class="n">next</span><span class="p">()</span>
    <span class="n">nangle</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">lines</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">split</span><span class="p">()[</span><span class="mi">0</span><span class="p">])</span>

    <span class="n">fields</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="n">angles</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="k">while</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="nb">len</span><span class="p">(</span><span class="n">lines</span><span class="p">)</span> <span class="ow">and</span> <span class="nb">len</span><span class="p">(</span><span class="n">fields</span><span class="p">)</span> <span class="o">&lt;</span> <span class="mi">3</span><span class="o">*</span><span class="n">nangle</span><span class="p">:</span>
        <span class="n">i</span> <span class="o">+=</span> <span class="mi">1</span>
        <span class="n">fields</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">lines</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">split</span><span class="p">())</span>
        
    <span class="k">for</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">,</span> <span class="n">c</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">fields</span><span class="p">[::</span><span class="mi">3</span><span class="p">],</span> <span class="n">fields</span><span class="p">[</span><span class="mi">1</span><span class="p">::</span><span class="mi">3</span><span class="p">],</span> <span class="n">fields</span><span class="p">[</span><span class="mi">2</span><span class="p">::</span><span class="mi">3</span><span class="p">]):</span>
        <span class="n">a</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
        <span class="n">b</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">b</span><span class="p">)</span>
        <span class="n">c</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">c</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">rev_sort_index</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">a</span> <span class="o">=</span> <span class="n">rev_sort_index</span><span class="p">[</span><span class="n">a</span><span class="p">]</span>
            <span class="n">b</span> <span class="o">=</span> <span class="n">rev_sort_index</span><span class="p">[</span><span class="n">b</span><span class="p">]</span>
            <span class="n">c</span> <span class="o">=</span> <span class="n">rev_sort_index</span><span class="p">[</span><span class="n">c</span><span class="p">]</span>
        <span class="n">angles</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">a</span><span class="p">,</span><span class="n">b</span><span class="p">,</span><span class="n">c</span><span class="p">))</span>

    <span class="k">return</span> <span class="n">angles</span>

<div class="viewcode-block" id="read_psf"><a class="viewcode-back" href="../../cp2k.html#quippy.cp2k.read_psf">[docs]</a><span class="k">def</span> <span class="nf">read_psf</span><span class="p">(</span><span class="n">psf</span><span class="p">,</span> <span class="n">rev_sort_index</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Read the PSF topology file from the filename `psf`.</span>

<span class="sd">       Returns a pair of lists `(bonds, angles)`. `bonds` is a list</span>
<span class="sd">       of pairs of bonded atom indices `(i, j)` and `angles` is a list</span>
<span class="sd">       of triplets `(i, j, k)`. Order is same as that in PSF file</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">lines</span>  <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="n">psf</span><span class="p">)</span><span class="o">.</span><span class="n">readlines</span><span class="p">()</span>
    <span class="n">bonds</span>  <span class="o">=</span> <span class="n">read_psf_bonds</span><span class="p">(</span><span class="n">lines</span><span class="p">,</span> <span class="n">rev_sort_index</span><span class="p">)</span>
    <span class="n">angles</span> <span class="o">=</span> <span class="n">read_psf_angles</span><span class="p">(</span><span class="n">lines</span><span class="p">,</span> <span class="n">rev_sort_index</span><span class="p">)</span>

    <span class="k">return</span> <span class="n">bonds</span><span class="p">,</span> <span class="n">angles</span></div>


<div class="viewcode-block" id="apply_cp2k_sort_order"><a class="viewcode-back" href="../../cp2k.html#quippy.cp2k.apply_cp2k_sort_order">[docs]</a><span class="k">def</span> <span class="nf">apply_cp2k_sort_order</span><span class="p">(</span><span class="n">at</span><span class="p">,</span> <span class="n">rev_sort_index_filename</span><span class="o">=</span><span class="s1">&#39;quip_rev_sort_index&#39;</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Reorder atoms in `at` so that they match rev_sort_index read from file.</span>

<span class="sd">       Returns sort_index and rev_sort_index arrays.&quot;&quot;&quot;</span>

    <span class="n">fields</span> <span class="o">=</span> <span class="p">[</span><span class="nb">int</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="nb">open</span><span class="p">(</span><span class="n">rev_sort_index_filename</span><span class="p">)</span><span class="o">.</span><span class="n">read</span><span class="p">()</span><span class="o">.</span><span class="n">split</span><span class="p">()</span> <span class="p">]</span>
    <span class="n">rev_sort_index</span> <span class="o">=</span> <span class="n">farray</span><span class="p">(</span><span class="n">fields</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">int32</span><span class="p">)</span>
    <span class="n">rev_sort_index_copy</span> <span class="o">=</span> <span class="n">rev_sort_index</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
    <span class="n">sort_index</span> <span class="o">=</span> <span class="n">farray</span><span class="p">(</span><span class="n">frange</span><span class="p">(</span><span class="n">at</span><span class="o">.</span><span class="n">n</span><span class="p">),</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">int32</span><span class="p">)</span>
    <span class="n">insertion_sort</span><span class="p">(</span><span class="n">rev_sort_index_copy</span><span class="p">,</span> <span class="n">sort_index</span><span class="p">)</span>
    <span class="n">at</span><span class="o">.</span><span class="n">add_property</span><span class="p">(</span><span class="s1">&#39;rev_sort_index&#39;</span><span class="p">,</span> <span class="n">rev_sort_index</span><span class="p">)</span>
    <span class="n">at</span><span class="o">.</span><span class="n">sort</span><span class="p">(</span><span class="s1">&#39;rev_sort_index&#39;</span><span class="p">)</span>

    <span class="k">return</span> <span class="n">sort_index</span><span class="p">,</span> <span class="n">rev_sort_index</span></div>


<div class="viewcode-block" id="bonds_from_avgpos"><a class="viewcode-back" href="../../cp2k.html#quippy.cp2k.bonds_from_avgpos">[docs]</a><span class="k">def</span> <span class="nf">bonds_from_avgpos</span><span class="p">(</span><span class="n">at</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Compute connectivity of `at` using `avgpos` positions.</span>

<span class="sd">    A copy of `at` is made to avoid changing its connect</span>
<span class="sd">    object. Returns list of bonds as a set.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    
    <span class="n">at_avg</span> <span class="o">=</span> <span class="n">at</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
    <span class="n">at_avg</span><span class="o">.</span><span class="n">pos</span><span class="p">[</span><span class="o">...</span><span class="p">]</span> <span class="o">=</span> <span class="n">at_avg</span><span class="o">.</span><span class="n">avgpos</span><span class="p">[</span><span class="o">...</span><span class="p">]</span>
    <span class="n">at_avg</span><span class="o">.</span><span class="n">calc_connect</span><span class="p">()</span>
    <span class="k">return</span> <span class="nb">set</span><span class="p">(</span><span class="n">at_avg</span><span class="o">.</span><span class="n">neighbours</span><span class="o">.</span><span class="n">pairs</span><span class="p">())</span></div>


<div class="viewcode-block" id="qmmm_link_bonds"><a class="viewcode-back" href="../../cp2k.html#quippy.cp2k.qmmm_link_bonds">[docs]</a><span class="k">def</span> <span class="nf">qmmm_link_bonds</span><span class="p">(</span><span class="n">at</span><span class="p">,</span> <span class="n">run_suffix</span><span class="o">=</span><span class="s2">&quot;&quot;</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Return the set of QM/MM link bonds in `at`.</span>

<span class="sd">    Bonds are computed from average positions using `bonds_from_avgpos()`.</span>
<span class="sd">    `run_suffix` is used to find the relevant `cluster_mark` property.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="n">bonds</span> <span class="o">=</span> <span class="n">bonds_from_avgpos</span><span class="p">(</span><span class="n">at</span><span class="p">)</span>
    <span class="n">mark</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">at</span><span class="p">,</span> <span class="s1">&#39;cluster_mark&#39;</span><span class="o">+</span><span class="n">run_suffix</span><span class="p">)</span>
    <span class="n">cut_bonds</span> <span class="o">=</span> <span class="p">[</span> <span class="p">(</span><span class="n">i</span><span class="p">,</span><span class="n">j</span><span class="p">)</span> <span class="k">for</span> <span class="p">(</span><span class="n">i</span><span class="p">,</span><span class="n">j</span><span class="p">)</span> <span class="ow">in</span> <span class="n">bonds</span> <span class="k">if</span> <span class="p">(</span><span class="n">mark</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)</span> <span class="o">!=</span> <span class="p">(</span><span class="n">mark</span><span class="p">[</span><span class="n">j</span><span class="p">]</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)</span> <span class="p">]</span>

    <span class="k">return</span> <span class="nb">set</span><span class="p">(</span><span class="n">cut_bonds</span><span class="p">)</span></div>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2008-2016, James Kermode

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>